package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.controller.vo.*;
import com.ruoyi.system.controller.vo.gnpz.*;
import com.ruoyi.system.controller.vo.info.CarInfoSys;
import com.ruoyi.system.domain.CarInfo;
import com.ruoyi.system.domain.CarPartFastener;
import com.ruoyi.system.domain.CardPartAnalyse;
import com.ruoyi.system.service.ICarFileService;
import com.ruoyi.system.service.ICarInfoService;
import com.ruoyi.system.service.ICarPartFastenerService;
import com.ruoyi.system.service.ICardPartAnalyseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆-信息Controller
 *
 * @author ruoyi
 * @date 2022-03-13
 */
@Slf4j
@Controller
@RequestMapping("/system/car/info")
public class CarInfoController extends BaseController {
    private String prefix = "system/car/info";

    @Autowired
    private ICarInfoService carInfoService;
    @Resource
    private ICarFileService iCarFileService;
    @Resource private ICardPartAnalyseService iCardPartAnalyseService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询车辆-信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarInfo carInfo) {
        startPage();
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆-信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "车辆-信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarInfo carInfo) {
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        return util.exportExcel(list, "车辆-信息数据");
    }

    /**
     * 新增车辆-信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆-信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "车辆-信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarInfo carInfo) {
        return toAjax(carInfoService.insertCarInfo(carInfo));
    }

    /**
     * 修改车辆-信息
     */
    @RequiresPermissions("system:info:edit")
    @GetMapping("/edit/{carId}")
    public String edit(Long carInfoId, @PathVariable("carId") Long carId, Long menuId, String busType, ModelMap mmap) {
        CarInfo carInfo = null;
        if (carInfoId != null) {
            carInfo = carInfoService.selectCarInfoByCarInfoId(carInfoId);
        } else {
            carInfo = new CarInfo();
            carInfo.setConfigType(busType);
            carInfo.setCarId(carId);
            carInfo.setMenuId(menuId);
            List<CarInfo> carInfos = carInfoService.selectCarInfoList(carInfo);
            if (CollectionUtils.isNotEmpty(carInfos)) {
                if (carInfos.size() > 1) {
                    throw new ServiceException("查询到多条记录");
                }
                carInfo = carInfos.get(0);
            }
        }
        if (carInfo.getCarInfoId() == null && StringUtils.isNotBlank(busType)) {
            if (StringUtils.equals(busType, CarInfoType.CAR_SYS_BEAR_GEARBOX)) {
                CarInfoSys disassembleSys = new CarInfoSys();
                disassembleSys.setSerialNumber(ShiroUtils.randomSalt());
                carInfo.setInfo(JSON.toJSONString(disassembleSys));
            } else if (StringUtils.equals(busType, CarInfoType.CAR_SYS_BEAR_GEARBOX_PART)) {
                CarInfoSys disassembleSys = new CarInfoSys();
                disassembleSys.setSerialNumber(ShiroUtils.randomSalt());
                carInfo.setInfo(JSON.toJSONString(disassembleSys));
            } else if (StringUtils.equals(busType, CarInfoType.CAR_SYS)) {
                DisassembleSys disassembleSys = new DisassembleSys();
                disassembleSys.setSerialNumber(ShiroUtils.randomSalt());
                carInfo.setInfo(JSON.toJSONString(disassembleSys));
            } else if (StringUtils.equals(busType, CarInfoType.CAR_PART)) {
                DisassemblePart disassemblePart = new DisassemblePart();
                disassemblePart.setSerialNumber(ShiroUtils.randomSalt());
                carInfo.setInfo(JSON.toJSONString(disassemblePart));
            } else {
                carInfo.setInfo(new JSONObject().toJSONString());
            }
            carInfoService.insertCarInfo(carInfo);
            logger.debug("新建车辆信息carId={},busType={} carInfoId={}", carId, busType, carInfo.getCarInfoId());
        }
        if (StringUtils.isBlank(carInfo.getInfo())) {
            carInfo.setInfo(new JSONObject().toJSONString());
        }
        Class<CarInfoItem> carInfoType = getCarInfoType(carInfo.getConfigType());
        if (carInfoType != null) {
            carInfo.setInfoMap(JSON.parseObject(carInfo.getInfo(), carInfoType));
        }
        mmap.put("carInfo", carInfo);
        /*部件分析*/
        CardPartAnalyse cardPartAnalyse = new CardPartAnalyse();
        cardPartAnalyse.setCardInfoId(carInfo.getCarInfoId());
        mmap.put("partAnalyses",iCardPartAnalyseService.selectCardPartAnalyseList(cardPartAnalyse));
        /*紧固件*/
        CarPartFastener carPartFastener = new CarPartFastener();
        carPartFastener.setCardInfoId(carInfo.getCarInfoId());
        //紧固
        carPartFastener.setFastenerType("0");
        mmap.put("fasteners0",iCarPartFastenerService.selectCarPartFastenerList(carPartFastener));
        //定位
        carPartFastener.setFastenerType("1");
        mmap.put("fasteners1",iCarPartFastenerService.selectCarPartFastenerList(carPartFastener));
        //细节
        carPartFastener.setFastenerType("2");
        mmap.put("fasteners2",iCarPartFastenerService.selectCarPartFastenerList(carPartFastener));
        return prefix + "/" + busType;
    }
    @Resource private ICarPartFastenerService iCarPartFastenerService;

    /** 根据类型获取具体实体类 */
    private <T extends CarInfoItem> Class<T> getCarInfoType(String configType) {
        Class class1 = null;
        switch (configType) {
            case CarInfoType.CAR_DLCS:
                class1 = CarInfoDlCs.class;
                break;
            case CarInfoType.CAR_DLCS_X:
                class1 = CarInfoDlcsX.class;
                break;
            case CarInfoType.CAR_DPXJ:
                class1 = CarInfoDpxj.class;
                break;
            case CarInfoType.CAR_JS:
                class1 = CarInfoJs.class;
                break;

            case CarInfoType.CAR_GNPZ:
                class1 = CarInfoGnPz.class;
                break;
            case CarInfoType.CAR_GNPZ_ZM:
                class1 = CarInfoGnpzZm.class;
                break;
            case CarInfoType.CAR_GNPZ_WS:
                class1 = CarInfoGnpzWs.class;
                break;
            case CarInfoType.CAR_GNPZ_NS:
                class1 = CarInfoGnpzNs.class;
                break;
            case CarInfoType.CAR_GNPZ_CK:
                class1 = CarInfoGnpzCk.class;
                break;

            case CarInfoType.CAR_AQ:
                class1 = CarInfoAq.class;
                break;
            case CarInfoType.CAR_XJ:
                class1 = CarInfoXj.class;
                break;
            case CarInfoType.CAR_CC:
                class1 = CarInfoCc.class;
                break;
            case CarInfoType.CAR_ZL:
                class1 = CarInfoZl.class;
                break;
            case CarInfoType.CAR_ZCCS:
                class1 = CarInfoZccs.class;
                break;
            case CarInfoType.CAR_WWCL:
                class1 = CarInfoWwCl.class;
                break;
            case CarInfoType.CAR_NBCL:
                class1 = CarInfoNbCl.class;
                break;
            case CarInfoType.CAR_WGTP:
                class1 = CarInfoWgTp.class;
                break;
            case CarInfoType.CAR_NBTP:
                class1 = CarInfoNbTp.class;
                break;
            case CarInfoType.CAR_ZMTP:
                class1 = CarInfoZmTp.class;
                break;
            case CarInfoType.CAR_ZCTP:
                class1 = CarInfoZcTp.class;
                break;
            case CarInfoType.CAR_SYS:
                class1 = DisassembleSys.class;
                break;
            case CarInfoType.CAR_PART:
                class1 = DisassemblePart.class;
                break;
            case CarInfoType.CAR_SYS_BEAR_GEARBOX:
                class1 = CarInfoSys.class;
                break;
            case CarInfoType.CAR_SYS_BEAR_GEARBOX_PART:
                class1 = CarInfoSys.class;
                break;
            default:
                log.warn("未知的汽车信息类型：configType={}", configType);
        }
        return class1;
    }


    /** 汽车详情类型 */
    interface CarInfoType {
        /** 底盘悬架 */
        String CAR_DPXJ = "car_dpxj";
        /** 驾驶 */
        String CAR_JS = "car_js";
        /** 动力参数 */
        String CAR_DLCS = "car_dlcs";
        /** 动力参数 新能源*/
        String CAR_DLCS_X = "car_dlcs_x";

        /** 功能配置 */
        String CAR_GNPZ = "car_gnpz";
        /** 功能配置 照明 */
        String CAR_GNPZ_ZM = "car_gnpz_zm";
        /** 功能配置 外饰 */
        String CAR_GNPZ_WS = "car_gnpz_ws";
        /** 功能配置 内饰 */
        String CAR_GNPZ_NS = "car_gnpz_ns";
        /** 功能配置 操控 */
        String CAR_GNPZ_CK = "car_gnpz_ck";

        /** 安全配置 */
        String CAR_AQ = "car_aq";
        /** 悬架 */
        String CAR_XJ = "car_xj";
        /** 尺寸 */
        String CAR_CC = "car_cc";
        /** 重量 */
        String CAR_ZL = "car_zl";

        /** 整车基础参数 */
        String CAR_ZCCS = "car_zccs";

        /** 外围测量 */
        String CAR_WWCL = "car_wwcl";
        /** 内部测量 */
        String CAR_NBCL = "car_nbcl";
        /** 外观图片 */
        String CAR_WGTP = "car_wgtp";
        /** 内饰图片 */
        String CAR_NBTP = "car_nbtp";
        /** 照明图片 */
        String CAR_ZMTP = "car_zmtp";
        /** 整车图片 */
        String CAR_ZCTP = "car_zctp";
        /** 拆解系统 */
        String CAR_SYS = "car_sys";
        /** 拆解部件 */
        String CAR_PART = "car_part";


        /** 拆解系统-轴承类/变速箱 */
        String CAR_SYS_BEAR_GEARBOX = "car_sys_bear_gearbox";
        /** 拆解部件-轴承类/变速箱 */
        String CAR_SYS_BEAR_GEARBOX_PART = "car_sys_bear_gearbox_part";
    }

    /**
     * 修改保存车辆-信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "车辆-信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarInfo carInfo) {
//        carInfo.setInfo(carInfo.getInfoMap() == null ? new JSONObject().toJSONString() : JSON.toJSONString(carInfo.getInfoMap()));
        if (carInfo.getCarInfoId() == null) {
            CarInfo query = new CarInfo();
            query.setConfigType(carInfo.getConfigType());
            query.setCarId(carInfo.getCarId());
            List<CarInfo> carInfos = carInfoService.selectCarInfoList(query);
            if (CollectionUtils.isNotEmpty(carInfos)) {
                carInfo.setCarInfoId(carInfos.get(0).getCarInfoId());
            }
        }
        if (carInfo.getCarInfoId() == null) {
            carInfo.setCreateBy(getLoginName());
            return toAjax(carInfoService.insertCarInfo(carInfo));
        } else {
            carInfo.setUpdateBy(getLoginName());
            return toAjax(carInfoService.updateCarInfo(carInfo));
        }
    }

    /**
     * 删除车辆-信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "车辆-信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carInfoService.deleteCarInfoByCarInfoIds(ids));
    }

}
